/**
 * Created by IntelliJ IDEA.
 * User: p.popov
 * Date: 11.03.13
 * Time: 12:24
 * To change this template use File | Settings | File Templates.
 */
package {

	import flash.geom.Point;

//import slicesPanels.*

import starling.core.Starling;
import starling.display.Image;
import starling.textures.Texture;

public class SimpleImage extends Image {
    private var loadCallBack:Function;
    protected var resourceId:String;
    private var loaded:Boolean;
    private var destroyed:Boolean;
    private var isUsed:Boolean;
    private static var emptyTexture:Texture = Texture.empty(32, 32);

    public function SimpleImage(resId:String) {
        super(emptyTexture);
        this.resourceId = prepareResId(resId);
        loadTexture();
    }


    protected function loadTexture():void {
       // ResourceCachesCollection.getTexturesCache().getResourceById(resourceId, setTexture);
	    var moonTexture:Texture = AtlasHolder.instance.atlas.getTexture(resourceId);
		setTexture(moonTexture);
	}

    protected function prepareResId(resId:String):String {
        var result:String = resId;
        if (result.indexOf(".png") > -1) {
            result = result.split(".")[0];
        }
        return result;
    }

    public function setLoadCallback(callback:Function):void {
        loadCallBack = callback;
        if (loaded) {
            if (loadCallBack != null) {
                loadCallBack();
            }
        }
    }

    public function setScale(scaleX:Number, scaleY:Number):void {
        this.scaleX = scaleX;
        this.scaleY = scaleY;
    }


    protected function setTexture(loadTexture:Texture):void {
        texture = loadTexture;
        readjustSize();
        loaded = true;
        if (loadCallBack != null) {
            loadCallBack();
        }
    }

    public function getResourceId():String {
        return resourceId;
    }

    public function setPosition(x:Number, y:Number):void {
        this.x = x;
        this.y = y;
    }

    public function destroy():void {
        loadCallBack = null;
        destroyed = true;
        //if (isUsed) {
            //SimpleImagesPool.getInstance().returnImage(this);
       // }
    }

    public function setSize(width:Number, height:Number):void {
        this.width = width;
        this.height = height;
    }


    public function isLoaded():Boolean {
        return loaded;
    }


    public function changeImage(texturesName:String):void {
        if (resourceId == texturesName) return;
        resourceId = texturesName;
        loaded = false;
        //ResourceCachesCollection.getTexturesCache().getResourceById(resourceId, setTexture);
    }

    public function setPositionPoint(point:Point):void {
        x = point.x;
        y = point.y;
    }

    public function setPositionPointAnimated(point:Point, time:Number):void {
        Starling.juggler.tween(this, time, {x: point.x});
        Starling.juggler.tween(this, time, {y: point.y});

    }

    /** как выяснилось не всегда отдает корректный результат
     * почему - не понятно
     * @deprecated
     * */
    public function getTextureNativeWidth():Number {
        return loaded && texture ? texture.nativeWidth : 0;
    }

    /** как выяснилось не всегда отдает корректный результат
     * почему - не понятно
     * @deprecated
     * */
    public function getTextureNativeHeight():Number {
        return loaded && texture ? texture.nativeHeight : 0;
    }


    public function getSize():Point {
        return new Point(width, height);
    }

    public function isDestroyed():Boolean {
        return destroyed;
    }


    internal function setUsed(value:Boolean):void {
        isUsed = value;
    }


    public function getPositionPoint():Point {
        return new Point(x, y);
    }
	
	/* INTERFACE ru.mail.games.BB.modules.gui.IHintable */
	
	public function enableTouch():void 
	{
		
	}
}
}
